package com.hht.tools.touchlib

import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.widget.ImageView
import androidx.appcompat.widget.LinearLayoutCompat
import com.hht.toolanduilibs.showLog
import com.hht.toolanduilibs.showToast
import com.hht.tools.R
import com.hht.touchlibs.UserTouchEvent
import com.hht.touchlibs.UserTouchManager


/**
 * Author:chen'hu
 * Time:2024/6/18 9:53
 * Description:This is TouchView
 */
class TouchView(private val mContext: Context, attrs: AttributeSet? = null) :
    LinearLayoutCompat(mContext, attrs), UserTouchEvent {
    private lateinit var mHomeIv: ImageView
    private lateinit var mWbIv: ImageView
    private lateinit var mTaskIv: ImageView
    private lateinit var mBackIv: ImageView
    private lateinit var onTouchMove: (x: Int, y: Int) -> Unit

    fun setOnTouchMove(touchMove: (x: Int, y: Int) -> Unit) {
        this.onTouchMove = touchMove
    }

    init {
        initView()
    }

    private fun initView() {
        inflate(mContext, R.layout.touch_view_layout, this)
        mHomeIv = findViewById(R.id.touch_home_iv)
        mWbIv = findViewById(R.id.touch_wb_iv)
        mTaskIv = findViewById(R.id.touch_task_iv)
        mBackIv = findViewById(R.id.touch_back_iv)

        UserTouchManager.addLayoutViewTouchEvent(mContext, this, this)
        UserTouchManager.addChileViewTouchEvent(mContext, mHomeIv, this)
        UserTouchManager.addChileViewTouchEvent(mContext, mWbIv, this)
        UserTouchManager.addChileViewTouchEvent(mContext, mTaskIv, this)
        UserTouchManager.addChileViewTouchEvent(mContext, mBackIv, this)
    }

    override fun onTouchClick(view: View) {
        when (view.id) {
            R.id.touch_home_iv -> "Home click".showToast(mContext)

            R.id.touch_wb_iv -> "Wb click".showToast(mContext)

            R.id.touch_task_iv -> "Task click".showToast(mContext)

            R.id.touch_back_iv -> "Back click".showToast(mContext)
        }
    }

    override fun onTouchMove(view: View, x: Int, y: Int) {
        onTouchMove.invoke(x,y)
    }

    override fun onTouchCancel(view: View) {

    }
}